//
//  DiggerStockListController.m
//  Stock
//
//  Created by blacksun on 11-7-5.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "DiggerStockListController.h"


@implementation DiggerStockListController
@synthesize title2Label;
@synthesize title3Label;
@synthesize title1Label;



-(void)queryStocks{
    NSString *uri = [NSString stringWithFormat:@"dig/pvdig?id=%@&start=%d",key,currentPage*kRecordPerRequest];
    [quotationCenterService queryQuotationStocksWithUri:uri];
}

-(void)viewDidLoad{
    [super viewDidLoad];
    if ([key isEqualToString:@"VR"]) {
        title3Label.text = @"成交增幅";
    }else if ([key isEqualToString:@"VRD"]) {
        title3Label.text = @"成交减幅";
    }else if ([key isEqualToString:@"VCR"]) {
        title2Label.text = @"放量天数";
        title2Label.frame = CGRectMake(170.0, 12.0, 70.0, 20.0);
        title3Label.text = @"阶段涨幅";
    }else if ([key isEqualToString:@"VCRD"]) {
        title2Label.text = @"缩量天数";
        title2Label.frame = CGRectMake(170.0, 12.0, 70.0, 20.0);
        title3Label.text = @"阶段涨幅";
    }else if ([key isEqualToString:@"PCR"]) {
        title2Label.text = @"连续天数";
        title2Label.frame = CGRectMake(170.0, 12.0, 70.0, 20.0);
        title3Label.text = @"阶段涨幅";
    }else if ([key isEqualToString:@"PCRD"]) {
        title2Label.text = @"连续天数";
        title2Label.frame = CGRectMake(170.0, 12.0, 70.0, 20.0);
        title3Label.text = @"阶段跌幅";
    }else if ([key isEqualToString:@"PH"]) {
        //title3Label.text = @"换手率";
        title3Label.hidden = YES;
        title1Label.frame = CGRectMake(110.0, 10.0, 90.0, 20.0);
        title2Label.frame = CGRectMake(220.0, 10.0, 90.0, 20.0);
        
    }else if ([key isEqualToString:@"PL"]) {
        //title3Label.text = @"换手率";
        title3Label.hidden = YES;
        title1Label.frame = CGRectMake(110.0, 10.0, 90.0, 20.0);
        title2Label.frame = CGRectMake(220.0, 10.0, 90.0, 20.0);
    }
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"DiggerStockList";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        
        UIColor *clearColor = [UIColor clearColor];
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(4.0, 10.0, 90.0, 20.0)];
        nameLabel.backgroundColor = clearColor;
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.tag = kStockNameTag;
        UILabel *todayPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(90.0, 10.0, 60.0, 20.0)];
        todayPriceLabel.backgroundColor = clearColor;
        todayPriceLabel.tag = kStockTodayPriceTag;
        todayPriceLabel.textAlignment = UITextAlignmentRight;
        
        UILabel *changeRateLabel = [[UILabel alloc] initWithFrame:CGRectMake(162.0, 10.0, 60.0, 20.0)];
        changeRateLabel.backgroundColor = clearColor;
        changeRateLabel.tag = kStockChangeRateTag;
        changeRateLabel.textAlignment = UITextAlignmentRight;
        
        UILabel *fNetLabel = [[UILabel alloc] initWithFrame:CGRectMake(230.0, 10.0, 85.0, 20.0)];
        fNetLabel.backgroundColor = clearColor;
        fNetLabel.tag = kStockChangePriceTag;
        fNetLabel.textAlignment = UITextAlignmentRight;
        
        
        [cell.contentView addSubview:nameLabel];
        [cell.contentView addSubview:todayPriceLabel];
        [cell.contentView addSubview:changeRateLabel];
        [cell.contentView addSubview:fNetLabel];
        
        [nameLabel release];
        [todayPriceLabel release];
        [changeRateLabel release];
        [fNetLabel release];
        
    }
    NSInteger index = [indexPath row];
    NSString *imageName = @"cellbg2.png";
    if (index%2==1) {
        imageName = @"cellbg1.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    QuotationStock *currentStock = [quotationStocks objectAtIndex:index];
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:kStockNameTag];
    UILabel *todyLabel = (UILabel *)[cell viewWithTag:kStockTodayPriceTag];
    UILabel *fNetLabel = (UILabel *)[cell viewWithTag:kStockChangePriceTag];
    UILabel *rateLabel = (UILabel *)[cell viewWithTag:kStockChangeRateTag];
    
    NSString *text3 = @"";
    NSString *text4 = @"";
    
    if ([key isEqualToString:@"VR"]) {
        text3 = currentStock.changedRate;
        text4 = currentStock.percent;
    }else if ([key isEqualToString:@"VRD"]) {
        text3 = currentStock.changedRate;
        text4 = currentStock.percent;
    }else if ([key isEqualToString:@"VCR"]) {
        text3 = [currentStock.days stringValue];
        text4 = currentStock.percent;
    }else if ([key isEqualToString:@"VCRD"]) {
        text3 = [currentStock.days stringValue];
        text4 = currentStock.percent;
    }else if ([key isEqualToString:@"PCR"]) {
        text3 = [currentStock.days stringValue];
        text4 = currentStock.percent;
    }else if ([key isEqualToString:@"PCRD"]) {
        text3 = [currentStock.days stringValue];
        text4 = currentStock.percent;
    }else if ([key isEqualToString:@"PH"]) {
        text3 = currentStock.changedRate;
        //text4 = currentStock.turnover;
        text4 = @"";
        todyLabel.frame = CGRectMake(110.0, 10.0, 90.0, 20.0);
        rateLabel.frame = CGRectMake(220.0, 10.0, 90.0, 20.0);
    }else if ([key isEqualToString:@"PL"]) {
        text3 = currentStock.changedRate;
        //text4 = currentStock.turnover;
        text4 = @"";
        todyLabel.frame = CGRectMake(110.0, 10.0, 90.0, 20.0);
        rateLabel.frame = CGRectMake(220.0, 10.0, 90.0, 20.0);
    }
    
    UIColor *redColor =[UIColor redColor];
    UIColor *greenColor  =[UIColor greenColor];    
    todyLabel.textColor = [UIColor whiteColor];
    rateLabel.textColor = [text3 hasPrefix:@"-"]?greenColor:redColor;
    fNetLabel.textColor = [text4 hasPrefix:@"-"]?greenColor:redColor;
    
    
    
    nameLabel.text = currentStock.name;
    if (currentStock.closingPrice && currentStock.closingPrice != 0) {
        todyLabel.text = [currentStock.lastClosingPrice stringValue];
        rateLabel.text = text3; 
        fNetLabel.text = text4;
    }else{
        todyLabel.text = @"--";
        rateLabel.text = @"--";  
        fNetLabel.text = @"--";
    }
    return cell;
}

-(void)viewDidUnload{
    [super viewDidUnload];
    self.title2Label = nil;
    self.title3Label = nil;
    self.title1Label = nil;
}
-(void)dealloc{
    self.title2Label = nil;
    self.title3Label = nil;
    self.title1Label = nil;
    [super dealloc];
}
@end
